# 导包
import unittest
import requests
from parameterized import parameterized
import utils
import app
from api import login
from lib.dbutil import DBUtil


# 创建测试类
class Test_login(unittest.TestCase):
    # 初始化
    TOKEN = None
    ID = None

    # 类前置处理
    @classmethod
    def setUpClass(cls) -> None:
        # 实例化类
        cls.login_api = login.LoginApi()
        # 实现登录
        response = cls.login_api.login(app.login_data)
        print(response.json())
        # 提取登录状态并保存
        Test_login.TOKEN = "Bearer " + response.json().get("data")
        print(Test_login.TOKEN)
        # 清理数据
        DBUtil.exe_sql("delete from bs_user WHERE mobile = '17759710556';")

    # 后置类
    @classmethod
    def tearDownClass(cls) -> None:
        # 清理数据
        DBUtil.exe_sql("delete from bs_user WHERE mobile = '17759710556';")

    # 前置处理
    def setUp(self) -> None:
        # 实例化类
        self.staff_api = login.LoginApi()

    # 后置处理
    def tearDown(self) -> None:
        pass

    # 获取员工列表
    def test01_get_userlist(self):
        response = self.staff_api.login_getuserlist(Test_login.TOKEN)
        print(response.json())
        # 断言
        utils.duanyan(self, response)

    # 添加员工
    def test02_add_user(self):
        response = self.staff_api.insert_user(app.add_data, Test_login.TOKEN)
        print(response.json())
        # 提取id
        Test_login.ID = response.json().get("data").get("id")
        print(Test_login.ID)

        utils.duanyan(self, response)

    # 修改员工
    def test03_put_user(self):
        response = self.staff_api.updata_user(Test_login.ID, app.put_data, Test_login.TOKEN)
        print(response.json())
        utils.duanyan(self, response)

    # 查看员工
    def test04_look(self):
        response = self.staff_api.select_user(Test_login.ID, Test_login.TOKEN)
        print(response.json())
        utils.duanyan(self, response)

    # 删除员工
    def test05_dele(self):
        response = self.staff_api.delete_user(Test_login.ID, Test_login.TOKEN)
        print(response.json())
        utils.duanyan(self, response)
